package com.shishi.fast.common.util;

import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: point
 * @类注解: 时间处理工具类
 * @author: zyh
 * @create: 2020-07-12
 */
public class DateTimeUtils {

    /**
     * 获取当前时间--用于生成单号
     *
     * @return
     */
    public static String getNowDateString() {
        SimpleDateFormat format = new SimpleDateFormat("-yyMMdd-");
        return format.format(new Date());
    }

    public static String getNowTimeString() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(new Date());
    }

    /**
     * 获取最近n天的日期
     *
     * @return
     */
    private static Date getDateAdd(int days) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, -days);
        return c.getTime();
    }

    /**
     * 例如获取当前时间的前 n 分钟时间
     *
     * @return
     */
    public static Date getMinuteReduce(int minute) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -minute);// n分钟之前的时间
        return beforeTime.getTime();
    }

    /**
     * 例如获取指定时间的前 n 分钟时间
     *
     * @return
     */
    public static Date getMinuteReduceByDate(Date date, int minute) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.setTime(date);
        beforeTime.add(Calendar.MINUTE, -minute);// n分钟之前的时间
        return beforeTime.getTime();
    }

    /**
     * 例如获取当前时间的前 n 分钟时间
     *
     * @return
     */
    public static String getMinuteReduceString(int minute) {
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -minute);// n分钟之前的时间
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return formatter.format(beforeTime.getTime());
    }


    /**
     * 获取指定天数前到现在的日期数组
     *
     * @return
     */
    public static List<String> getDaysBetween(int days) {
        List<String> dayss = new ArrayList<>();
        Calendar start = Calendar.getInstance();
        start.setTime(getDateAdd(days));
        long startTIme = start.getTimeInMillis();
        Calendar end = Calendar.getInstance();
        end.setTime(new Date());
        Long endTime = end.getTimeInMillis();
        Long oneDay = 1000 * 60 * 60 * 24L;
        Long time = startTIme;
        while (time <= endTime) {
            Date d = new Date(time);
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            dayss.add(df.format(d));
            time += oneDay;
        }
        return dayss;
    }

    /**
     * 将秒数转为xx小时xx分xx秒格式
     *
     * @return
     */
    public static String secondTimeFormat(Long secondTime) { //最近几天日期
        if (secondTime == null || secondTime == 0) {
            return "暂无时长";
        }
        long h = secondTime / 3600;
        long m = secondTime % 3600 / 60;
        long s = secondTime % 60; //不足60的就是秒，够60就是分
        return h + "小时" + m + "分钟" + s + "秒";
    }

    /**
     * 将字符串时间转位字符串日期
     *
     * @return
     */
    public static String parseStringTime2StringDate(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = formatter.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return formatter.format(dateTime);
    }

    /**
     * 将字符串转为日期
     *
     * @return
     */
    public static Date parseString2Date(String date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime = null;
        try {
            dateTime = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    /**
     * 将时间转字符串
     *
     * @return
     */
    public static String parseDate2DateString(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(date);
    }

    /**
     * 将字符串转位时间
     *
     * @return
     */
    public static Date parseString2DateTime(String date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateTime = null;
        try {
            dateTime = formatter.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dateTime;
    }

    //计算两个时间相差的秒数
    public static int getTime(Date startTime, Date endTime) {
        long eTime = endTime.getTime();
        long sTime = startTime.getTime();
        long diff = (eTime - sTime) / 1000;
        return (int) diff;
    }

    /**
     * 获取指定天数前的时间
     *
     * @return
     */
    public static String getBeforeDayDateTime(int day) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_MONTH, -day);
        String endDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now.getTime());
        return endDate;
    }

    /**
     * 获取指定天数前的日期
     *
     * @return
     */
    public static String getBeforeDayDate(int day) {
        Calendar now = Calendar.getInstance();
        now.add(Calendar.DAY_OF_MONTH, -day);
        String endDate = new SimpleDateFormat("yyyy-MM-dd").format(now.getTime());
        return endDate;
    }

    /**
     * 比较日期大小
     * 日期1 > 日期2
     *
     * @return
     */
    public static boolean compareToDate(Date date1, Date date2) {
        long long1 = date1.getTime();
        long long2 = date2.getTime();
        return long1 > long2;
    }

    /**
     * 获取时间区间内所有日期
     *
     * @return
     */
    public static List<String> findDates(String beginTime, String endTime) {
        List<String> allDate = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Date dBegin = null;
        Date dEnd = null;
        try {
            dBegin = sdf.parse(beginTime);
            dEnd = sdf.parse(endTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        allDate.add(sdf.format(dBegin));
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(dBegin);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(dEnd);
        // 测试此日期是否在指定日期之后
        while (dEnd.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            allDate.add(sdf.format(calBegin.getTime()));
        }
        return allDate;
    }

    /**
     * 获取跨月的报表两个日期之间所有月份
     *
     * @param startDate
     * @param endDate
     * @return：YYYY-MM
     */
    public static List<String> getReportMonthBetweenDate(Date startDate, Date endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        // 声明保存日期集合
        List<String> list = new ArrayList<String>();
        try {
            list.add(sdf.format(DateUtils.addMonths(startDate, -1)));
            //用Calendar 进行日期比较判断
            Calendar calendar = Calendar.getInstance();
            while (startDate.getTime() <= endDate.getTime()) {
                // 把日期添加到集合
                list.add(sdf.format(startDate));
                // 设置日期
                calendar.setTime(startDate);
                //把日期增加一天
                calendar.add(Calendar.MONTH, 1);
                // 获取增加后的日期
                startDate = calendar.getTime();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 获取两个日期的相隔天数 --by时间格式
     *
     * @param beginDate 起始时间
     * @param endDate   结束时间
     * @return
     * @throws ParseException
     */
    public static Integer getBetweenDayByDate(Date beginDate, Date endDate) {

        long betweenDate = (endDate.getTime() - beginDate.getTime()) / (1000 * 60 * 60 * 24);
        return (int) betweenDate;
    }

    /**
     * 获取随机时间
     */
    public static Date getRandomTime(Date date, int minHour, int maxHour) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int hour = new Random().nextInt(maxHour - minHour + 1) + minHour;
        cal.set(Calendar.HOUR_OF_DAY, hour);
        cal.set(Calendar.MINUTE, new Random().nextInt(60));
        return cal.getTime();
    }

    /**
     * 获取随机结束时间
     */
    public static Date getRandomEndTime(Date startTime, double minHours, double maxHours) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startTime);
        int minutes = (int) (60 * (minHours + new Random().nextDouble() * (maxHours - minHours)));
        cal.add(Calendar.MINUTE, minutes);
        return cal.getTime();
    }

}